package com.zjw.socket.demo.starter.messageService.service.impl;

import com.zjw.socket.demo.starter.annotation.core.Bean;
import com.zjw.socket.demo.starter.messageService.service.IUserDataBase;
import com.zjw.socket.demo.starter.messageService.User;

import java.util.ArrayList;
import java.util.List;

@Bean(name = "SimpleUserDataBase")
public class SimpleUserDataBase implements IUserDataBase {

  private final List<User> userList = new ArrayList<>();
  @Override
  public User queryUserByUsername(long username) {
    for (User user : userList) {
      if (user.username == username){
        return user;
      }
    }
    return null;
  }

  @Override
  public boolean insertUser(User user) {
    userList.remove(user);
    userList.add(user);
    return true;
  }

  @Override
  public void updateUser(User user) {
    int indexOf = userList.indexOf(user);
    if (indexOf >= 0){
      userList.set(indexOf,user);
    }
  }

  @Override
  public boolean removeUser(User user) {
    userList.remove(user);
    return false;
  }
}
